package com.HomeWorkhsp.chapter13.arrays_;

import java.util.Arrays;
import java.util.Comparator;
@SuppressWarnings({"all"})
public class ArraysSortCustom {
    public static void main(String[] args) {
        int[] arr = {1, -1, 6, -10, 22, 5};
        bubble02(arr, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                int i1 = (Integer)o1;
                int i2 = (Integer)o2;
                return i2 - i1;
            }
        });

        System.out.println(Arrays.toString(arr));
    }

    //原版冒泡排序
    public static void bubble(int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j + 1]){
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    //自定义顺序版冒泡排序，传入接口参数，通过匿名内部类实现自定义
    public static void bubble02(int[] arr, Comparator c){
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (c.compare(arr[j], arr[j + 1]) > 0){
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}
